'use strict';

const Controller = require('egg').Controller;

class ResourcesController extends Controller {
  async page() {
    const resource = await this.ctx.service.system.resources.index();
    const router = await this.ctx.service.system.routers.index();
    const fs = require('fs');
    const path = require('path');

    const public_dir = path.join(this.app.baseDir, 'app/public');
    let icons;
    try {
      icons = JSON.parse(fs.readFileSync(public_dir + '/js/jquery-fonticonpicker/materialdesignicons.json'));
    } catch (e) {
      icons = {
        name: 'materialdesignicons',
        glyphs: [
          {
            css: 'mdi mdi-access-point',
            code: 'f002',
          },
          {
            css: 'mdi mdi-access-point-network',
            code: 'f003',
          }],
      };
    }

    resource.rows.forEach( res => {
      res.routerDesc = res.description ? res.description : res['router.routerDesc'];
    })

    resource.rows.push({id: 0, routerDesc: '根目录'});
    await this.ctx.render('/system/resource.tpl', { resources: resource.rows, routers: router.rows, icons: JSON.stringify(icons) });
  }
  async index() {
    // const { msg, msgType } = this.ctx.query;
    // 显示所有记录
    // 支持从 /system/account/index?offset=0&limit=20 来分页
    const { ctx } = this;
    const query = ctx.query;

    query.page = query.page === 'NaN' ? 0 : query.page;
    query.offset = query.offset ? parseInt(query.offset) : 0;
    query.limit = query.limit ? parseInt(query.limit) : 999;
    query.sortOrder = query.sortOrder || 'desc';

    const data = await ctx.service.system.resources.index(query);
    data.rows.forEach( res => {
      res.routerDesc = res.description ? res.description : res['router.routerDesc'];
    })

    ctx.set('Content-Type', 'application/json');
    if (typeof (data) === 'string') {
      ctx.state = 500;
      ctx.body = data;
    } else {
      data.total = data.count;// 兼容bootstrap-table 记录总条数字段
      ctx.state = 200;
      ctx.body = data;
    }
  }

  async show() {
    // /posts/:id	post
    const { ctx } = this;
    const id = ctx.params.id;
    ctx.set('Content-Type', 'application/json');
    if (!id) {
      ctx.state = 403;
      ctx.body = { code: 403, msg: '参数不正确！' };
    } else {
      const data = await ctx.service.system.resources.show(id);
      ctx.state = 200;
      ctx.body = { code: 200, data };
    }
  }

  async create() {
    // /posts	posts
    // post 方式新增
    const { ctx } = this;
    const data = ctx.request.body;
    ctx.set('Content-Type', 'application/json');
    try {

      await ctx.service.system.resources.new(data);
      ctx.state = 201;
      ctx.body = { code: 201, msg: '新增成功！' };
    } catch (e) {
      ctx.state = 500;
      ctx.body = { code: 500, msg: '新增失败！' + e.message };
    }
  }

  async update() {
    // /posts/:id	post
    // put 方式更新
    const { ctx } = this;
    const { name, value, pk } = ctx.request.body;
    const id = pk || ctx.params.id;
    const data = pk ? { [name]: value } : ctx.request.body;

    ctx.set('Content-Type', 'application/json');
    if (!id || !data) {
      ctx.state = 403;
      ctx.body = { code: 403, msg: '参数不正确！' };
    } else {
      try {
        await ctx.service.system.resources.edit(id, data);
        ctx.state = 200;
        ctx.body = { code: 200, msg: '更新成功！' };
      } catch (e) {
        ctx.state = 500;
        ctx.body = { code: 500, msg: '更新失败！' + e.message };
      }
    }
  }

  async destroy() {
    // /posts/:id	post
    // delete 方式删除
    const { ctx } = this;
    const id = ctx.params.id;
    ctx.set('Content-Type', 'application/json');

    if (!id) {
      ctx.state = 403;
      ctx.body = { code: 403, msg: '参数不正确！' };
    } else {
      try {
        await ctx.service.system.resources.destroy(id);
        ctx.state = 200;
        ctx.body = { code: 200, msg: '记录删除成功！' };
      } catch (e) {
        ctx.state = 500;
        ctx.body = { code: 500, msg: '记录删除失败！' + e.message };
      }
    }
  }
}

module.exports = ResourcesController;
